package com.xiebiao.mystarter;

import com.xiebiao.mystarter.service.DefaultTimerImpl;
import com.xiebiao.mystarter.service.Timer;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <pre>
 *     可以按条件来初始化bean：
 *     1、如果bean不存在，则初始化
 *     2、判断某个配置是否存在，存在则初始化bean
 *     3、依赖多个类决定是否初始化bean
 * </pre>
 *
 * @Description: XXX
 * @Author: Bill Xie
 * @Create: 2025/6/16 14:40
 **/
@Configuration
@EnableConfigurationProperties(MyStarterProperties.class)
public class MyStarterConfiguration {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private MyStarterProperties myStarterProperties;

    /**
     * 如果没有bean，则初始化
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean()
    public Timer timer() {
        logger.info("Loading myStarter property:{}", myStarterProperties.getOther());
        return new DefaultTimerImpl();
    }
//
//    /**
//     * 2.判断配置标识，决定是否初始化bean，或者初始化不同实现的bean
//     *
//     * @return
//     */
//    @Bean
//    @ConditionalOnProperty(havingValue = "true", prefix = "mystarter", name = {"showTimer"})
//    public Timer timer2() {
//        return new DefaultTimerImpl2();
//    }

//    /**
//     * 判断classpath中是否存在指定的类
//     *
//     * @return
//     */
//    @Bean
//    @ConditionalOnClass(name = {"com.xiebiao.starter.Demo"})
//    public Timer timer3() {
//        return new DefaultTimerImpl2();
//    }
}
